package com.example.lijuan.myapplication.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.lijuan.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class CarInfoListAdapter extends BaseAdapter {

    private List<Integer> data;
    private LayoutInflater layoutInflater;
    private Context context;

    public CarInfoListAdapter(Context context, ArrayList<Integer> data) {
        this.context = context;
        this.data = data;
        this.layoutInflater = LayoutInflater.from(context);
    }


    public final class Zujian {
        public TextView title, info;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    /**
     * 获得某一位置的数据
     */
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    /**
     * 获得唯一标识
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Zujian zujian = null;
        if (convertView == null) {
            zujian = new Zujian();
            //获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.fragment_car_info_list_item, null);
            zujian.title = (TextView) convertView.findViewById(R.id.title);
            zujian.info = (TextView) convertView.findViewById(R.id.info);
            convertView.setTag(zujian);
        } else {
            zujian = (Zujian) convertView.getTag();
        }

        //绑定数据
        switch (position) {
            case 0:
                zujian.title.setText("里程");
                zujian.info.setText(data.get(0) + "公里");
                break;
            case 1:
                zujian.title.setText("油量");
                zujian.info.setText(data.get(1) + "升");
                break;
            case 2:
                zujian.title.setText("速度");
                zujian.info.setText(data.get(2) + "公里/小时");
                break;
        }
        return convertView;
    }

}
